﻿using System;
using System.Data;
using ITS.Core.Domain.Common;
using System.Linq;


namespace ITS.DAL.Concrete.EF.Common
{
    public enum BizIdType
    {
        ContentItemID = 1
    }

    /// <summary>
    /// This class support us to get Business Id for Job, Resume, JKAccount, EPAccount
    /// </summary>
    public class BizCodeGenerator
    {
        /// <summary>
        /// Return Business Code String
        /// </summary>
        /// <param name="type">Job, Resume, JKAccount, EPAccount, CustReservation</param>
        /// <returns>BusinessCode</returns>
        public static string GetBusinessCode(BizIdType type)
        {
            int valueBizIdType = (int)type;
            long nextval = NextVal(valueBizIdType);

            string surfix = String.Empty;

            switch (type)
            {
                case BizIdType.ContentItemID:
                    surfix = String.Empty;
                    break;
                default:
                    surfix = String.Empty;
                    break;
            }
            return String.Format("{0:X7}{1}", nextval, surfix);
        }

        /// <summary>
        /// Get current value and increase to next value
        /// </summary>
        /// <param name="type">1,2,3,4</param>
        /// <returns>Formatted String</returns>
        public static Int64 NextVal(int type)
        {
            using (EnterpriseContext context = new EnterpriseContext())
            {
                BusinessIdSeq result = (from o in context.BusinessIdSeqs
                                        where o.IdType == type
                                        select o).FirstOrDefault();
                Int64 nextval = result.NextVal.Value;
                //Increase the next value in database
                result.NextVal += 1;
                context.Entry(result).State = EntityState.Modified;
                context.SaveChanges();
                return nextval;
            }
        }
    }
}
